package com.zh.note.huawei.twohundred.数字的排列or数字反转打印;

/**
 * https://dream.blog.csdn.net/article/details/129826832
 */

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        try (Scanner s = new Scanner(System.in)) {
            // int n = s.nextInt();
            int n = 3;
            solve(n);
        }
    }

    private static void solve(int n) {
        boolean asc = true;
        LinkedList<int[]> arrList = new LinkedList<>();
        int count = 1;
        for (int i = 1; i <= n; ++i) {
            int[] arr = new int[i];
            int k = (asc == true ? 0 : arr.length - 1);
            for (int j = 0; j < arr.length; j++) {
                arr[k] = count++;
                k = k + (asc == true ? 1 : -1);
            }

            arrList.push(arr);
            asc = !asc;
        }
        // 最终结果
        List<String> resList = new LinkedList<>();
        StringBuilder sb = new StringBuilder();//保存首尾要加的空格
        StringBuilder row;

        for (int[] ints : arrList) {
            row = new StringBuilder(); // 首先设置首尾空格
            row.append(sb);
            for (int j = 0; j < ints.length; j++) {
                int num = ints[j];
                row.append(num);
                // 补充星号
                for (int k = 0; k < 4 - (num + "").length(); k++) {
                    row.append("*");
                }
                // 数字之间要加空行
                if (j != ints.length - 1) {
                    row.append("    ");
                }
            }
            resList.add(row.toString());
            // 设置下一次首尾空格
            sb.append("    ");
        }
        // 从后往前输出
        for (int i = resList.size() - 1; i >= 0; --i) {
            System.out.println(resList.get(i));
        }
    }


}

